package com.pvdnc.nsocket.protocol;

import com.pvdnc.nsocket.log.SLog;
import com.pvdnc.nsocket.protocol.base.Packet;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class Decoder extends ByteToMessageDecoder {
    private static final SLog LOG=new SLog(Decoder.class);

    private static final AtomicReference<Decoder> sInstance=new AtomicReference<>();

    public static Decoder getInstance(){
        if(sInstance.get()==null)
            sInstance.set(new Decoder());
        return sInstance.get();
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> outList) throws Exception {
        try{
            Packet packet= PacketCodec.getInstance().decode(in);
            LOG.d("decoded packet:"+packet);
            outList.add(packet);//这里就要返回正确的类型了
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
